package com.hkjtgc.pcr_app.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.gson.Gson
import com.hkjtgc.pcr_app.R
import com.hkjtgc.pcr_app.adapter.BindingMachineAdapter
import com.hkjtgc.pcr_app.adapter.MACHINE_TYPE_GREEN
import com.hkjtgc.pcr_app.adapter.MACHINE_TYPE_RED
import com.hkjtgc.pcr_app.adapter.MachineBindingVo
import com.hkjtgc.pcr_app.database.DatabaseManager
import com.hkjtgc.pcr_app.entity.bean.GetCardBoxBean
import com.hkjtgc.pcr_app.entity.bean.GetSimpleBean
import com.hkjtgc.pcr_app.model.TodoInfoDO
import com.hkjtgc.pcr_app.util.Constant
import com.hkjtgc.pcr_app.util.SharedPreferencesUtils
import com.hkjtgc.pcr_app.util.ToastUtil
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

@RequiresApi(Build.VERSION_CODES.M)
class BindingMachineActivity : AppCompatActivity() {

    private lateinit var recycleView : RecyclerView
    private lateinit var createDate : TextView
    private lateinit var profileName : TextView
    private lateinit var profileSEX : TextView
    private lateinit var simpleType : TextView
    private lateinit var simpleName : TextView
    private lateinit var cardType : TextView
    private lateinit var cardName : TextView
    var operatorName: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_binding_machine)
        init()
        if(intent.getStringExtra("data") == "false") initBinding() else initHomeFragment()
    }


    private fun initHomeFragment(){
        val todoInfo : TodoInfoDO = Gson().fromJson(SharedPreferencesUtils.getSPInstance(this).getSP(Constant.TODO_INFO),TodoInfoDO::class.java)
        operatorName = todoInfo.operatorName

        createDate.text = todoInfo.createDate
        cardType.text = todoInfo.cardBoxType
        cardName.text = todoInfo.cardBoxName
        profileName.text = todoInfo.profileName

        profileSEX.text = todoInfo.profileSex
        simpleName.text = todoInfo.simpleName
        simpleType.text = todoInfo.simpleType
    }


    @SuppressLint("MissingInflatedId", "SetTextI18n", "CutPasteId")
    private fun initBinding() {
//        val cardBoxBean = Gson().fromJson(SharedPreferencesUtils.getSPInstance(this).getSP(Constant.CARD_BOX_INFORMATION),GetCardBoxBean::class.java)
//        val simpleBean = Gson().fromJson(SharedPreferencesUtils.getSPInstance(this).getSP(Constant.SIMPLE_INFORMATION),GetSimpleBean::class.java)
        val cardBoxBean = GetCardBoxBean("核酸", "A类卡盒")
        val simpleBean = GetSimpleBean("核酸", "A类样品管", "张三", "MEDICAL")
        operatorName = SharedPreferencesUtils.getSPInstance(this).getSP(Constant.USER_NAME)

        createDate.text = SharedPreferencesUtils.getSPInstance(this).getSP(Constant.CREATE_DATE)
        cardType.text = "${findViewById<TextView>(R.id.card_type).text}${cardBoxBean.type}"
        cardName.text = "${findViewById<TextView>(R.id.card_name).text}${cardBoxBean.name}"
        profileName.text = "${findViewById<TextView>(R.id.user_name).text}${simpleBean.profileName}"

        profileSEX.text = "${findViewById<TextView>(R.id.sex).text}${simpleBean.profileSex}"
        simpleName.text = "${findViewById<TextView>(R.id.simple_name).text}${simpleBean.simpleName}"
        simpleType.text = "${findViewById<TextView>(R.id.simple_type).text}${simpleBean.simpleType}"
    }


    private fun init(){
        createDate = findViewById(R.id.create_date)
        cardType = findViewById(R.id.card_type)
        cardName = findViewById(R.id.card_name)
        profileName = findViewById(R.id.user_name)

        profileSEX = findViewById(R.id.sex)
        simpleName = findViewById(R.id.simple_name)
        simpleType = findViewById(R.id.simple_type)
        recycleView = findViewById(R.id.machine_choose_id)

        val machineList = mutableListOf<MachineBindingVo>(MachineBindingVo("一号", MACHINE_TYPE_RED),MachineBindingVo("二号",
            MACHINE_TYPE_GREEN),MachineBindingVo("三号", MACHINE_TYPE_RED),MachineBindingVo("四号",MACHINE_TYPE_RED),MachineBindingVo("一号", MACHINE_TYPE_RED),MachineBindingVo("二号",
            MACHINE_TYPE_GREEN),MachineBindingVo("三号", MACHINE_TYPE_RED),MachineBindingVo("四号",MACHINE_TYPE_RED),MachineBindingVo("一号", MACHINE_TYPE_RED),MachineBindingVo("二号",
            MACHINE_TYPE_GREEN),MachineBindingVo("三号", MACHINE_TYPE_RED),MachineBindingVo("四号",MACHINE_TYPE_RED),MachineBindingVo("一号", MACHINE_TYPE_RED),MachineBindingVo("二号",
            MACHINE_TYPE_GREEN),MachineBindingVo("三号", MACHINE_TYPE_RED),MachineBindingVo("四号",MACHINE_TYPE_RED))

        val layoutManger = StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL)
        recycleView.layoutManager = layoutManger
        val adapter = BindingMachineAdapter(machineList)
        recycleView.adapter = adapter

        findViewById<TextView>(R.id.save_exit_button).setOnClickListener{
            val todoInfo = TodoInfoDO(null,operatorName,createDate.text.toString(),simpleType.text.toString(),
                simpleName.text.toString(),cardType.text.toString(),cardName.text.toString(),profileName.text.toString(),
                profileSEX.text.toString())
            GlobalScope.launch {
                try {
                    DatabaseManager.db.todoDao().insert(todoInfo)

                    
                }catch (_: Exception) {

                }
            }
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        }
    }


}